package com.game.display  
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * 
	 * @author cheryZeng
	 */
	public class AutoGCSprite extends Sprite
	{
		
		//生成remove后自动清除内存的sprite
		private var lis:Array=new Array();//定义一数组保存侦听;
		/**
		 * 
		 */
		public function AutoGCSprite()
		{
			addEventListener(Event.REMOVED,remove);
		}
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			lis.push([type,listener,useCapture]);
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		private function remove(e:Event):void
		{
			if (e.currentTarget != e.target)
			{
				return;
			}
			
			//删除子对象
			//trace("删除前有子对象",numChildren);
			while (numChildren > 0)
			{
				removeChildAt(0);
			}
		//	trace("删除后有子对象",numChildren);
			//删除动态属性
			for (var k:String in this)
			{
				//trace("删除属性",k);
				delete this[k];
			}
			
			//删除侦听
			for (var i:uint=0; i<lis.length; i++)
			{
				//trace("删除Listener",lis[i][0]);
				removeEventListener(lis[i][0],lis[i][1],lis[i][2]);
			}
			lis = null;
		}
	}
}